﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.ViewInfo;
using DevExpress.XtraTreeList.Nodes.Operations;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Columns;
using QLPhanQuyen.BO;
using QLKetNoi;


namespace QLPhanQuyen.GUI
{
    public partial class SuaVaiTroForm : Office2007Form
    {
        VaiTroBO VTBO = new VaiTroBO();
        NhomChucNangBO NCNBO = new NhomChucNangBO();
        ChucNangBO CNBO = new ChucNangBO();
        ChucNangConBO CNCBO = new ChucNangConBO();
        VaiTro_ChucNangCon_QuyenHanBO VTCNCQHBO = new VaiTro_ChucNangCon_QuyenHanBO();
        QuyenHanBO QHBO = new QuyenHanBO();

        private string _tenVT;
        public SuaVaiTroForm(string tenVT)
        {
            InitializeComponent();
            _tenVT = tenVT;
        }

        private void SuaVaiTroForm_Load(object sender, EventArgs e)
        {
            txtMaVaiTro.Text = VTBO.LayMaVaiTroTheoTen(_tenVT);
            txtTenVaiTro.Text = _tenVT;
            txtDienGiai.Text = VTBO.LayDienGiaiVaiTroTheoTen(_tenVT);
            checkKichHoat.Checked = VTBO.LayTrangThaiVaiTroTheoTen(_tenVT);
            
            KhoiTaotlSuaVaiTro();
            DanhSachChecktlSuaVaiTro(VTBO.LayMaVaiTroTheoTen(_tenVT));
        }

        private bool SuaVaiTro()
        {
            if (txtTenVaiTro.Text != "")
            {
                return VTBO.SuaVaiTro(txtMaVaiTro.Text, txtTenVaiTro.Text, txtDienGiai.Text, checkKichHoat.Checked);
            }
            else
            {
                MessageBox.Show("Tên Vai Trò Không được để trống");
                return false;
            }
        }

        private bool SuaPhanQuyenVaiTro(string maVT)
        {
            return VTCNCQHBO.XoaTheoMaVaiTro(maVT);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (SuaVaiTro())
            {
                string maVT = VTBO.LayMaVaiTroTheoTen(_tenVT);
                if (SuaPhanQuyenVaiTro(maVT))
                {
                    ThemVaiTroChucNangConQuyenHan(maVT);
                    MessageBox.Show("Sửa Vai Trò Thành Công");
                }
            }
            KhoiTaotlSuaVaiTro();
            DanhSachChecktlSuaVaiTro(VTBO.LayMaVaiTroTheoTen(_tenVT));
        }

        private void ThemVaiTroChucNangConQuyenHan(string maVT, string maCNC, string maQH)
        {
            VAITRO_CHUCNANGCON_QUYENHAN VTCNCQH = new VAITRO_CHUCNANGCON_QUYENHAN();

            VTCNCQH.MaVaiTro = maVT;
            VTCNCQH.MaChucNangCon = maCNC;
            VTCNCQH.MaQuyenHan = maQH;

            VTCNCQHBO.ThemVT_CNC_QH(VTCNCQH);
        }


        private void CheckTatCaChoMotCot(TreeListNode node, TreeListColumn col)
        {
            if (Convert.ToBoolean(node.GetValue(col)))
            {
                if (node.HasChildren)
                {
                    for (int i = 0; i < node.Nodes.Count; i++)
                    {
                        node.Nodes[i].SetValue(col, isCheck.ValueChecked);
                        CheckTatCaChoMotCot(node.Nodes[i], col);
                    }
                }
            }
            else if (node.HasChildren)
            {
                for (int i = 0; i < node.Nodes.Count; i++)
                {
                    CheckTatCaChoMotCot(node.Nodes[i], col);
                }
            }
        }

        private void CheckTatCaChoMotNode(TreeListNode node)
        {
            node.SetValue(tlcolTatCa, isCheck.ValueChecked);
            node.SetValue(tlcolTruyCap, isCheck.ValueChecked);
            node.SetValue(tlcolThem, isCheck.ValueChecked);
            node.SetValue(tlcolXoa, isCheck.ValueChecked);
            node.SetValue(tlcolCapNhat, isCheck.ValueChecked);
            node.SetValue(tlcolIn, isCheck.ValueChecked);
            node.SetValue(tlcolNhap, isCheck.ValueChecked);
            node.SetValue(tlcolXuat, isCheck.ValueChecked);
        }
        private void UnCheckTatCaChoMotNode(TreeListNode node)
        {
            node.SetValue(tlcolTatCa, isCheck.ValueUnchecked);
            node.SetValue(tlcolTruyCap, isCheck.ValueUnchecked);
            node.SetValue(tlcolThem, isCheck.ValueUnchecked);
            node.SetValue(tlcolXoa, isCheck.ValueUnchecked);
            node.SetValue(tlcolCapNhat, isCheck.ValueUnchecked);
            node.SetValue(tlcolIn, isCheck.ValueUnchecked);
            node.SetValue(tlcolNhap, isCheck.ValueUnchecked);
            node.SetValue(tlcolXuat, isCheck.ValueUnchecked);
        }
        private void CheckTatCaCacNodeCon(TreeListNode node)
        {
            CheckTatCaChoMotNode(node);
            if (node.HasChildren)
            {
                for (int i = 0; i < node.Nodes.Count; i++)
                {
                    CheckTatCaChoMotNode(node.Nodes[i]);
                    if (node.Nodes[i].HasChildren)
                    {
                        CheckTatCaCacNodeCon(node.Nodes[i]);
                    }
                }
            }
        }

        private void UnCheckTatCaCacNodeCon(TreeListNode node)
        {
            if (node.HasChildren)
            {
                UnCheckTatCaChoMotNode(node);
                for (int i = 0; i < node.Nodes.Count; i++)
                {
                    UnCheckTatCaChoMotNode(node.Nodes[i]);
                    if (node.Nodes[i].HasChildren)
                    {
                        UnCheckTatCaCacNodeCon(node.Nodes[i]);
                    }
                }
            }
        }

        private void KiemTraCheckCotChoCay()
        {
            for (int i = 0; i < tlSuaVaiTro.Nodes.Count; i++)
            {
                CheckTatCaChoMotCot(tlSuaVaiTro.Nodes[i], tlcolTruyCap);
                CheckTatCaChoMotCot(tlSuaVaiTro.Nodes[i], tlcolThem);
                CheckTatCaChoMotCot(tlSuaVaiTro.Nodes[i], tlcolXoa);
                CheckTatCaChoMotCot(tlSuaVaiTro.Nodes[i], tlcolCapNhat);
                CheckTatCaChoMotCot(tlSuaVaiTro.Nodes[i], tlcolIn);
                CheckTatCaChoMotCot(tlSuaVaiTro.Nodes[i], tlcolNhap);
                CheckTatCaChoMotCot(tlSuaVaiTro.Nodes[i], tlcolXuat);
            }
        }

        public void KiemTraCheckTatCa()
        {
            for (int i = 0; i < tlSuaVaiTro.Nodes.Count; i++)
            {
                if (Convert.ToBoolean(tlSuaVaiTro.Nodes[i].GetValue(tlcolTatCa)))
                {
                    CheckTatCaCacNodeCon(tlSuaVaiTro.Nodes[i]);
                }
                else
                {
                    for (int j = 0; j < tlSuaVaiTro.Nodes[i].Nodes.Count; j++)
                    {
                        if (Convert.ToBoolean(tlSuaVaiTro.Nodes[i].Nodes[j].GetValue(tlcolTatCa)))
                        {
                            CheckTatCaCacNodeCon(tlSuaVaiTro.Nodes[i].Nodes[j]);
                        }
                        else
                        {
                            for (int k = 0; k < tlSuaVaiTro.Nodes[i].Nodes[j].Nodes.Count; k++)
                            {
                                if (Convert.ToBoolean(tlSuaVaiTro.Nodes[i].Nodes[j].Nodes[k].GetValue(tlcolTatCa)))
                                {
                                    CheckTatCaCacNodeCon(tlSuaVaiTro.Nodes[i].Nodes[j].Nodes[k]);
                                }
                            }
                        }
                    }
                }
            }
        }

        private void KhoiTaotlSuaVaiTro()
        {

            tlSuaVaiTro.BeginUnboundLoad();

            for (int i = 0; i < NCNBO.DSTenNhomChucNang().Count(); i++)
            {
                TreeListNode lv1 = tlSuaVaiTro.AppendNode(
                    new object[] { NCNBO.DSTenNhomChucNang()[i] }, null);

                string maNhom = NCNBO.LayMaTheoTen(NCNBO.DSTenNhomChucNang()[i]);
                for (int j = 0; j < CNBO.DSTenChucNangTheoNhom(maNhom).Count(); j++)
                {
                    TreeListNode lv2 = tlSuaVaiTro.AppendNode(
                        new object[] { CNBO.DSTenChucNangTheoNhom(maNhom)[j] }, lv1);

                    string maChucNang = CNBO.LayMaTheoTen(CNBO.DSTenChucNangTheoNhom(maNhom)[j]);
                    for (int k = 0; k < CNCBO.DSChucNangConTheoChucNang(maChucNang).Count(); k++)
                    {
                        TreeListNode lv3 = tlSuaVaiTro.AppendNode(
                            new object[] { CNCBO.DSChucNangConTheoChucNang(maChucNang)[k] }, lv2);
                    }
                }
            }

            tlSuaVaiTro.EndUnboundLoad();
        }

        private void tlSuaVaiTro_MouseUp(object sender, MouseEventArgs e)
        {
            KiemTraCheckTatCa();
            KiemTraCheckCotChoCay();
        }

        private List<string> DanhSachCheckCuaNode(TreeListNode node)
        {
            List<string> DanhSachCheckCuaNode = new List<string>();

            if (Convert.ToBoolean(node.GetValue(tlcolTruyCap)))
                DanhSachCheckCuaNode.Add("Truy Xuất");
            if (Convert.ToBoolean(node.GetValue(tlcolThem)))
                DanhSachCheckCuaNode.Add("Thêm");
            if (Convert.ToBoolean(node.GetValue(tlcolXoa)))
                DanhSachCheckCuaNode.Add("Xóa");
            if (Convert.ToBoolean(node.GetValue(tlcolCapNhat)))
                DanhSachCheckCuaNode.Add("Sửa");
            if (Convert.ToBoolean(node.GetValue(tlcolIn)))
                DanhSachCheckCuaNode.Add("In");
            if (Convert.ToBoolean(node.GetValue(tlcolXuat)))
                DanhSachCheckCuaNode.Add("Xuất");
            if (Convert.ToBoolean(node.GetValue(tlcolNhap)))
                DanhSachCheckCuaNode.Add("Nhập");

            if (DanhSachCheckCuaNode != null)
                return DanhSachCheckCuaNode;
            else
                return null;
        }

        private string LayTextNode(TreeListNode node)
        {
            return node.GetDisplayText(tlcolChucNang);
        }

        private void ThemVaiTroChucNangConQuyenHan(string maVT)
        {
            for (int pn = 0; pn < tlSuaVaiTro.Nodes.Count; pn++)   // pn == rootnode là node cha của cây
            {
                TreeListNode parent = tlSuaVaiTro.Nodes[pn];

                for (int cn = 0; cn < parent.Nodes.Count; cn++)     //child node == cn node con của cha
                {
                    TreeListNode child = parent.Nodes[cn];

                    for (int gcn = 0; gcn < child.Nodes.Count; gcn++)    //gcn == grandchildnode node chau
                    {
                        TreeListNode grandchild = child.Nodes[gcn];
                        if (DanhSachCheckCuaNode(grandchild) != null)
                        {
                            string maChucNangCon = CNCBO.LayMaTheoTen(LayTextNode(grandchild));
                            for (int i = 0; i < DanhSachCheckCuaNode(grandchild).Count; i++)
                            {
                                string maQuyenHan = QHBO.LayMaTheoTen(DanhSachCheckCuaNode(grandchild)[i]);
                                ThemVaiTroChucNangConQuyenHan(maVT, maChucNangCon, maQuyenHan);
                                
                            }
                        }
                    }
                }
            }
        }

        

        

        #region Hiển Thị lên màn hình
        private void DanhSachChecktlSuaVaiTro(string maVT)
        {
            for (int pn = 0; pn < tlSuaVaiTro.Nodes.Count; pn++)   // pn == rootnode là node cha của cây
            {
                TreeListNode parent = tlSuaVaiTro.Nodes[pn];

                for (int cn = 0; cn < parent.Nodes.Count; cn++)     //child node == cn node con của cha
                {
                    TreeListNode child = parent.Nodes[cn];

                    for (int gcn = 0; gcn < child.Nodes.Count; gcn++)    //gcn == grandchildnode node chau
                    {
                        TreeListNode grandchild = child.Nodes[gcn];
                        CheckChoNode(grandchild, maVT);
                    }
                }
            }
        }

        private void CheckChoNode(TreeListNode node, string maVT)
        {
            string tenCNC = LayTextNode(node);
            string maCNC = CNCBO.LayMaTheoTen(tenCNC);

            if (kiemTraCheckColumn(maVT, maCNC, tlcolTruyCap) == true)
                node.SetValue(tlcolTruyCap, isCheck.ValueChecked);
            else
                node.SetValue(tlcolTruyCap, isCheck.ValueUnchecked);

            if (kiemTraCheckColumn(maVT, maCNC, tlcolThem) == true)
                node.SetValue(tlcolThem, isCheck.ValueChecked);
            else
                node.SetValue(tlcolThem, isCheck.ValueUnchecked);

            if (kiemTraCheckColumn(maVT, maCNC, tlcolXoa) == true)
                node.SetValue(tlcolXoa, isCheck.ValueChecked);
            else
                node.SetValue(tlcolXoa, isCheck.ValueUnchecked);

            if (kiemTraCheckColumn(maVT, maCNC, tlcolCapNhat) == true)
                node.SetValue(tlcolCapNhat, isCheck.ValueChecked);
            else
                node.SetValue(tlcolCapNhat, isCheck.ValueUnchecked);

            if (kiemTraCheckColumn(maVT, maCNC, tlcolIn) == true)
                node.SetValue(tlcolIn, isCheck.ValueChecked);
            else
                node.SetValue(tlcolIn, isCheck.ValueUnchecked);

            if (kiemTraCheckColumn(maVT, maCNC, tlcolNhap) == true)
                node.SetValue(tlcolNhap, isCheck.ValueChecked);
            else
                node.SetValue(tlcolNhap, isCheck.ValueUnchecked);

            if (kiemTraCheckColumn(maVT, maCNC, tlcolXuat) == true)
                node.SetValue(tlcolXuat, isCheck.ValueChecked);
            else
                node.SetValue(tlcolXuat, isCheck.ValueUnchecked);
        }

        private bool kiemTraCheckColumn(string maVT, string maCNC, TreeListColumn col)
        {
            string tenQH = col.GetTextCaption();
            string maQH = QHBO.LayMaTheoTen(tenQH);
            if (VTCNCQHBO.KiemTraTonTai(maVT, maCNC, maQH) == true)
                return true;
            else
                return false;
        }
        #endregion
    }
}
